package com.defysoft.waukids.calendar;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import org.json.JSONObject;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.defysoft.waukids.BaseFragment;
import com.defysoft.waukids.Custom_Data_Type.WK_ATTEND_CHECK;
import com.defysoft.waukids.Data_Sharedpreferences;
import com.defysoft.waukids.Data_Type.WK_ATTEND;
import com.defysoft.waukids.Data_Type.WK_PROGRAM;
import com.defysoft.waukids.Data_Type.WK_STATE;
import com.defysoft.waukids.Data_Type.WK_TEACHER_STATE;
import com.defysoft.waukids.JsonParser;
import com.defysoft.waukids.PublicSharedpreferences;
import com.defysoft.waukids.R;
import com.defysoft.waukids.Sqlite_Connect;

public class Calendar_Main_Attend extends BaseFragment implements OnClickListener{
	View view;
	boolean msg_check = false;
    boolean beacon_msg_check = true;
	boolean refresh= false;
	long now = System.currentTimeMillis();
	Date date = new Date(now);
	SimpleDateFormat mYear = new SimpleDateFormat("yyyy");
	SimpleDateFormat mMonth = new SimpleDateFormat("MM");
	SimpleDateFormat mDay = new SimpleDateFormat("dd");
	EditText input;
	public Sqlite_Connect sqlite_Connect;
	int year = Integer.parseInt(mYear.format(date)),
	month =  Integer.parseInt(mMonth.format(date)), count, select_date;
	Calendar cal;
	ArrayList<String> mDayofNowMonth;
	TextView tv[] = new TextView[5];
	ListView lv;
	LinearLayout ly;
	Calendar_Adapter adapter;
	Beaconmsg_Adapter beacon_adapter;
	GridView gv;
	Chinese_Date cd ;
	PublicSharedpreferences psf;
	Data_Sharedpreferences.SF_Member member;
	Data_Sharedpreferences.SF_Student student;
	ArrayList<WK_PROGRAM>program;
	ArrayList<WK_ATTEND>attend;
	ArrayList<WK_STATE> beacon_msg;
	ArrayList<WK_TEACHER_STATE> teacher_state;
	ArrayList<WK_ATTEND_CHECK>att_check;
	private final int SUCCESS = 10;
	private final int FAIL = 9;
	private final int NETWORK_ERR = 8;
	private final int CHANGE_SUCCESS = 7;
	private final int BEACON_SUCCESS = 6;
	private final int MISSATTEND_SUCCESS= 5;
	private final int MISSATTEND_DELETE= 4;
	String student_code="",send_date="",choose_day="",subject ="",pk_att_code="";
	@Override
	public View onCreateView(LayoutInflater inflater,
			@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.calendar_main_fragment, null);	
		setStartData();
		
		return view;	
	}
	
	@Override
	public void setStartData() {
		super.setStartData();
		sqlite_Connect = new Sqlite_Connect(getActivity());
		gv = (GridView) view.findViewById(R.id.CALENDAR_MAIN_DATE);		
		tv[0] = (TextView) view.findViewById(R.id.CALENDAR_MAIN_TEXT);
		tv[1] = (TextView) view.findViewById(R.id.CALENDAR_PREV);
		tv[2] = (TextView) view.findViewById(R.id.CALENDAR_NEXT);
		tv[3] = (TextView) view.findViewById(R.id.CALENDAR_DATE);
		tv[4] = (TextView) view.findViewById(R.id.CALENDAR_MAIN_DELETE);
		lv = (ListView) view.findViewById(R.id.CALENDAR_MAIN_LIST);
		ly = (LinearLayout) view.findViewById(R.id.CALENDAR_MAIN_TEXT_CONTAINER);
		cd = new Chinese_Date();
		/*// 년
		year = Integer.parseInt(mYear.format(date));
		// 월
		month = Integer.parseInt(mMonth.format(date));*/
		cal = Calendar.getInstance();
		cal.set(year, month - 1, 1);
		
		psf = new PublicSharedpreferences(getActivity());
		psf.setMembersp();
		member= psf.getMembersp(); 
		if(member.Rank.equals("3")){
		psf.setStudentsp();
		student = psf.getStudentsp();
		}
		tv[1].setOnClickListener(this);
		tv[2].setOnClickListener(this);
		tv[4].setOnClickListener(this);
		teacher_state = new ArrayList<WK_TEACHER_STATE>();
		beacon_msg = new ArrayList<WK_STATE>();
		beacon_adapter = new Beaconmsg_Adapter();				
		lv.setAdapter(beacon_adapter);	
		startNetwork(0);
	}
		//초기 달력 셋팅
	void addSchedule() {
		try {
			student_code= "";
			send_date =(cal.get(Calendar.MONTH)+1)+"";
			if(send_date.length()==2){
				send_date = (cal.get(Calendar.MONTH)+1)+"";
			}else{
				send_date = "0"+(cal.get(Calendar.MONTH)+1)+"";		
			}
			if(!member.Rank.equals("3")){
				student_code = "";
			}else{
				student_code=student.Code;
			}
			
			JSONObject jsonObject = new JSONObject(publicHttpCallback(HTTP_URL,
					"code=" + CODE_CALENDAR_LIST 
					+"&FK_Sch_Att_Code=" + member.School
					+"&FK_Cla_Att_Code=" + member.Class
					+"&FK_Stu_Att_Code="+ student_code
					+"&Att_Date=" + cal.get(Calendar.YEAR)+"-"+send_date
					+"&rank="+member.Rank
					));
		

			program = JsonParser.program_list(jsonObject.getString("program"));
			
		if(member.Rank.equals("3")){
			attend = JsonParser.attend_list(jsonObject.getString("payload"));
		}else{
			att_check= JsonParser.att_check(jsonObject.getString("payload"));
		}
			if (jsonObject.getBoolean("success")) {
				onNetworkCallback(SUCCESS, "");
			} else {
				onNetworkCallback(FAIL, jsonObject.getString("message"));
			}
		} catch (Exception e) {
			onNetworkCallback(NETWORK_ERR, e.toString() +" where addSchedule()");
		}
	}
	
	void change_calender() {
		try {
			student_code= "";
			send_date =(cal.get(Calendar.MONTH)+1)+"";
			if(send_date.length()==2){
				send_date = (cal.get(Calendar.MONTH)+1)+"";
			}else{
				send_date = "0"+(cal.get(Calendar.MONTH)+1)+"";		
			}
			if(!member.Rank.equals("3")){
				student_code = "";
			}else{
				student_code=student.Code;
			}
			
			JSONObject jsonObject = new JSONObject(publicHttpCallback(HTTP_URL,
					"code=" + CODE_CALENDAR_LIST 
					+"&FK_Sch_Att_Code=" + member.School
					+"&FK_Cla_Att_Code=" + member.Class
					+"&FK_Stu_Att_Code="+ student_code
					+"&Att_Date=" + cal.get(Calendar.YEAR)+"-"+send_date
					+"&rank="+member.Rank
					));

			program = JsonParser.program_list(jsonObject.getString("program"));
		if(member.Rank.equals("3")){
			attend = JsonParser.attend_list(jsonObject.getString("payload"));
		}else{
			att_check= JsonParser.att_check(jsonObject.getString("payload"));
		}
			if (jsonObject.getBoolean("success")) {
				onNetworkCallback(CHANGE_SUCCESS, "");
			} else {
				onNetworkCallback(FAIL, jsonObject.getString("message"));
			}
		} catch (Exception e) {
			onNetworkCallback(NETWORK_ERR, e.toString() +" where change_calender()");
		}
	}

	//비콘 메세지 로드
	void beacon_state() {
	try {
		if(!member.Rank.equals("3")){
			student_code ="";
		}else{
			student_code=student.Code;
		}
		
		JSONObject jsonObject = new JSONObject(publicHttpCallback(HTTP_URL,
				"code=" + CODE_CALENDAR_BEACON 
				+"&FK_Sch_Att_Code=" + member.School
				+"&FK_Stu_Att_Code="+student_code
				+"&Att_Date=" + choose_day
				+"&rank=" + member.Rank
				+"&FK_Cla_Att_Code="+member.Class));
	
		if(member.Rank.equals("3")){
			beacon_msg = JsonParser.state_list(jsonObject.getString("payload"));
		}else{
			teacher_state = JsonParser.teacher_state_list(jsonObject.getString("payload"));
		}
		if (jsonObject.getBoolean("success")) {
			onNetworkCallback(BEACON_SUCCESS, "");
		} else {
			onNetworkCallback(FAIL, jsonObject.getString("message"));
		}
	} catch (Exception e) {
		onNetworkCallback(NETWORK_ERR, e.toString() + " where beacon_state()");
	}
}
	//결석사유
	void missattend_write() {	
		try{
			JSONObject jsonObject = new JSONObject(publicHttpCallback(HTTP_URL,
					"code=" + CODE_CALENDAR_MISSATTEND_WRITE
					+"&PK_Att_Code="+pk_att_code
					+"&FK_Sch_Att_Code=" + member.School
					+"&FK_Stu_Att_Code="+student_code
					+"&Att_Date=" + choose_day
					+"&FK_Cla_Att_Code="+member.Class
					+"&subject="+input.getText().toString()
					));

			if (jsonObject.getBoolean("success")) {
				onNetworkCallback(MISSATTEND_SUCCESS, jsonObject.getString("payload"));
			} else {
				onNetworkCallback(FAIL, jsonObject.getString("message"));
			}
		} catch (Exception e) {
			onNetworkCallback(NETWORK_ERR, e.toString() + " where missattend_write()");
		}
	}
	
	void missattend_delete(){
		try{
			JSONObject jsonObject = new JSONObject(publicHttpCallback(HTTP_URL,
					"code=" + CODE_CALENDAR_MISSATTEND_DELETE
					+"&PK_Att_Code="+pk_att_code
					));

			if (jsonObject.getBoolean("success")) {
				onNetworkCallback(MISSATTEND_DELETE, jsonObject.getString("payload"));
			} else {
				onNetworkCallback(FAIL, jsonObject.getString("message"));
			}
		} catch (Exception e) {
			onNetworkCallback(NETWORK_ERR, e.toString() + " where missattend_delete()");
		}
	}
	@Override
	public void startThread(int choice) {
		super.startThread(choice);
		final int cho = choice;
		firstThread  = new Thread(new Runnable() {	
			@Override
			public void run() {
				switch (cho) {
				case 0:
					addSchedule();
					break;		
				case 1:
					beacon_state();
					break;
				case 2:
					change_calender();
					break;
				case 3:
					missattend_write();
					break;
				case 4:
					missattend_delete();
					break;
				}
			}
		});
	
		firstThread.start();
		if(refresh==false){
		startDIal();
		}
	}

	@Override
	protected void handlingMessage(Message _message) {
		// TODO Auto-generated method stub
		super.handlingMessage(_message);
		switch (_message.what) {		
		case SUCCESS:			
			if (firstThread != null) {
				firstThread.interrupt();
			}
			ly.setVisibility(View.GONE);
			//달력 셋팅
			Calendar_Grid(0);
			adapter = new Calendar_Adapter();
			gv.setAdapter(adapter);
			endDIal();
			break;
		
		case CHANGE_SUCCESS:
			if (firstThread != null) {
				firstThread.interrupt();
			}
			ly.setVisibility(View.GONE);
			adapter.notifyDataSetChanged();
			beacon_adapter.notifyDataSetChanged();
			refresh=false;
			endDIal();
			break;
			
		case BEACON_SUCCESS:
			if (firstThread != null) {
				firstThread.interrupt();
			}
			
			if(beacon_msg_check==false){
				Handler mHandler = new Handler();
				mHandler.postDelayed(new Runnable() {
					
					@Override
					public void run() {
						// TODO Auto-generated method stub
						endDIal();
						
						beacon_adapter.notifyDataSetChanged();
						beacon_msg_check=true;
					}
				}, 1500);
							
			}else{
				Log.e("else", beacon_msg_check+"");

			}

			
			break;
		
		case MISSATTEND_SUCCESS:
			if (firstThread != null) {
				firstThread.interrupt();
			}
			Toast.makeText(getActivity(), (String) _message.obj, Toast.LENGTH_SHORT).show();
			refresh=true;
			startNetwork(2);
			break;
		case MISSATTEND_DELETE:
			if (firstThread != null) {
				firstThread.interrupt();
			}
			Toast.makeText(getActivity(), (String) _message.obj, Toast.LENGTH_SHORT).show();
			startNetwork(2);
			refresh=true;
			break;
		case FAIL:
			if (firstThread != null) {
				firstThread.interrupt();
			}
			Toast.makeText(getActivity(), (String) _message.obj, Toast.LENGTH_SHORT).show();
			endDIal();
			break;
			
		case NETWORK_ERR:
			if (firstThread != null) {
				firstThread.interrupt();
			}
			sqlite_Connect.insert_Err_Log((String)_message.obj,android.os.Build.VERSION.SDK_INT,"Calendat_Main_Attend", member.Email);
			endDIal();
			break;
		}
	}

	void Calendar_Grid(int count) {
		mDayofNowMonth = new ArrayList<String>();		
		cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH) + count, 1);		
		tv[3].setText(cal.get(Calendar.YEAR)+"년"+"-" +(cal.get(Calendar.MONTH)+1) + "월");
		
		int nNowMonthMaxDay = cal.getActualMaximum(Calendar.DATE);// 해당 달의 최대 날짜
		int nNowMonthStartWeek = cal.get(Calendar.DAY_OF_WEEK);// 해당 달 1일의 시작 위치

		// 앞의 빈공간 만들어줄 갯수
		int nDaysOfPrevMonth = 0;
		// 월요일로 시작되는 달이면
		if (nNowMonthStartWeek == 2) {
			nDaysOfPrevMonth = 1;
		}
		// 일요일로 시작되는 달이면
		else if (nNowMonthStartWeek == 1) {
			nDaysOfPrevMonth = 0;
		} else {
			nDaysOfPrevMonth = nNowMonthStartWeek - 1;
		}

		if (nDaysOfPrevMonth != 0) {
			for (int i = 0; i < nDaysOfPrevMonth; i++) {
				mDayofNowMonth.add("");
			}
		}

		for (int i = 0; i < nNowMonthMaxDay; i++) {
			mDayofNowMonth.add((i + 1) + "");
		}
		// 주
		int week = mDayofNowMonth.size() / 7;
		// 몇주 까지 있는지 계산
		week += mDayofNowMonth.size() % 7 != 0 ? 1 : 0;
		// 앞으로 남은 공백
		int mDayofAfterMonth = (week * 7) - mDayofNowMonth.size();// 공백 갯수
		// 어레이에 공백추가
		for (int i = 0; i < mDayofAfterMonth ; i++) {
			mDayofNowMonth.add("");
		}
			
	}

	class Calendar_Adapter extends ArrayAdapter<String> {

		public Calendar_Adapter() {
			super(getActivity(), android.R.layout.simple_list_item_1);
		}

		@Override
		public int getCount() {
			return mDayofNowMonth.size();
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			Calendar_Holder holder;
		final int pos = position;
			if (convertView == null) {
				holder = new Calendar_Holder();
				convertView = getActivity().getLayoutInflater().inflate(
						R.layout.calendar_grid_row, null);
				holder.date = (TextView) convertView.findViewById(R.id.CALENDAR_ROW_TEXT);
				holder.ly = (LinearLayout)convertView.findViewById(R.id.CALENDAR_ROW_LL);
				holder.iv = (ImageView)convertView.findViewById(R.id.CALENDAR_ROW_CHECK);
				holder.ly_state = (LinearLayout)convertView.findViewById(R.id.CALENDAR_ROW_STATE);
				int width = ((gv.getWidth())/7);
				int height =(int)((double)(gv.getWidth()/7)*0.75);				
				holder.ly.setLayoutParams(new LinearLayout.LayoutParams(width,height));
				convertView.setTag(holder);
			} else {				
				
				holder = (Calendar_Holder) convertView.getTag();	
				
			}
			
			String day = mDayofNowMonth.get(position);
			
			if(day.length()<2){				
				day ="0"+day;					
			}
			
			if(select_date == (position+1)){
				holder.ly.setBackgroundColor(0x778bc540);
			}else{
				holder.ly.setBackgroundColor(0xffffffff);
			}
			
			holder.date.setTextColor(0xff000000);
			holder.ly_state.setVisibility(View.GONE);
			cd.getHoliday((cal.get(Calendar.MONTH)+1));
			
						
			holder.date.setText(mDayofNowMonth.get(position));
			if(member.Rank.equals("3")){
				holder.iv.setVisibility(View.VISIBLE);
				holder.iv.setImageResource(R.drawable.check_off);
				if(mDayofNowMonth.get(position).equals("")){
					holder.iv.setVisibility(View.GONE);
				}
				
			//출결
				for (int j = 0; j < attend.size(); j++) {						
					if((cal.get(Calendar.YEAR)+"-"+send_date+"-"+day).equals(attend.get(j).Att_Date)){							
						if(attend.get(j).Att_Check.equals("0")){
							holder.iv.setImageResource(R.drawable.check_on);	
						}
					}
				}	
			}else{
				//선생님, 원장선생님 뷰
				for (int j = 0; j < att_check.size(); j++) {						
					if((cal.get(Calendar.YEAR)+"-"+send_date+"-"+day).equals(att_check.get(j).Att_Date)){							
						if(Integer.parseInt(att_check.get(j).attcheck)>0){
							holder.ly_state.setVisibility(View.VISIBLE);
							holder.ly_state.setBackgroundColor(0xffff0000);
						}
					}
				}	
			}
			
			holder.ly.setOnLongClickListener(new OnLongClickListener() {
				
				@Override
			public boolean onLongClick(View v) {
					pk_att_code = "";
					subject = "";
					select_date = (pos+1);
					String writeday =  mDayofNowMonth.get(pos);
					if(writeday.length()<2){
						writeday ="0"+writeday;	
					}
					choose_day = (cal.get(Calendar.YEAR)+"-"+send_date+"-"+writeday);
					
					if(member.Rank.equals("1") || member.Rank.equals("2")){
						Intent i = new Intent(getActivity(),CalendarAttendListActivity.class);
						i.putExtra("Att_Date", cal.get(Calendar.YEAR) + "-"
								+send_date+"-"+writeday);
						startActivity(i);
					}else{
					/*	for (int i = 0; i < attend.size(); i++) {
							if((cal.get(Calendar.YEAR)+"-"+send_date+"-"+writeday).equals(attend.get(i).Att_Date)){
								if(attend.get(i).Att_Check.equals("1")){
									pk_att_code = attend.get(i).PK_Att_Code;
									subject = attend.get(i).Att_Reason;
								}
							}
						}						
							dialog_write();*/
					}
					adapter.notifyDataSetChanged();
					return false;
				}
			});
			if(beacon_msg_check==true){	
			holder.ly.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					beacon_msg_check=false;
					select_date = (pos+1);
					
					ly.setVisibility(View.GONE);
					String writeday =  mDayofNowMonth.get(pos);
					if(writeday.length()<2){
						writeday ="0"+writeday;	
					}
					if(member.Rank.equals("1")||member.Rank.equals("2")){	
						startNetwork(1);
						choose_day = (cal.get(Calendar.YEAR)+"-"+send_date+"-"+writeday);
						ly.setVisibility(View.GONE);
						lv.setVisibility(View.VISIBLE);
						
						
						
				}else if(member.Rank.equals("3")){
					
						choose_day = (cal.get(Calendar.YEAR)+"-"+send_date+"-"+writeday);						
						//결석 사유
						for (int i = 0; i < attend.size(); i++) {
							if((cal.get(Calendar.YEAR)+"-"+send_date+"-"+writeday).equals(attend.get(i).Att_Date)){							
								if(attend.get(i).Att_Check.equals("1")){									
									lv.setVisibility(View.GONE);
									ly.setVisibility(View.VISIBLE);
									pk_att_code = attend.get(i).PK_Att_Code;
									tv[0].setText("▶ 결석사유 \n"+student.Name+" 어린이 "+ attend.get(i).Att_Reason+" "+attend.get(i).Att_Date);									
						}else{			
								if(member.Rank.equals("3")){
									startNetwork(1);
									ly.setVisibility(View.GONE);
									lv.setVisibility(View.VISIBLE);
									
									
									}								
								}
							}
						}
					}
					adapter.notifyDataSetChanged();
					
				}		
			});	
			 
			}
			if (position % 7 == 0) {
				holder.date.setTextColor(0xffff0000);
			}
			if (position % 7 == 6) {
				holder.date.setTextColor(0xff0066ff);	
			}
			if (position % 7 == 6 || position % 7 == 0){
				holder.iv.setVisibility(View.GONE);
			}
			
			//일정 휴일
			for (int i = 0; i < program.size(); i++) {
				
			 if((cal.get(Calendar.YEAR)+"-"+send_date+"-"+day).equals(program.get(i).Pro_Date)){
				if(program.get(i).Pro_Check.equals("1")){
					holder.iv.setVisibility(View.GONE);
					holder.date.setText(mDayofNowMonth.get(position)+"\n"+program.get(i).Pro_Reason);
					holder.date.setTextColor(0xffff0000);					
					holder.ly.setOnLongClickListener(null);
					
					}
			 	}
			}

			//휴일
			for (int j = 0; j < cd.date.size(); j++) {							
					if(mDayofNowMonth.get(position).equals(cd.date.get(j).day)){
						holder.date.setText(mDayofNowMonth.get(position)+"\n"+cd.date.get(j).name);
						holder.iv.setVisibility(View.GONE);
						holder.date.setTextColor(0xffff0000);	
						holder.ly.setOnLongClickListener(null);
						break;
					}
				}
			
			//오늘보다 이후인지 검사
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
			Date present;
			Date future;
			int compare;
			try {
				present = format.parse((cal.get(Calendar.YEAR)+"-"+mMonth.format(date)+"-"+mDay.format(date)));
				future = format.parse(cal.get(Calendar.YEAR)+"-"+send_date+"-"+day);
				
				compare = present.compareTo(future);
				if(compare<0){
					holder.iv.setVisibility(View.GONE);
					if(!member.Rank.equals("3")){
						holder.ly.setLongClickable(false);
					}
				}else{
					tv[4].setVisibility(View.GONE);
					if(member.Rank.equals("3")){
					holder.ly.setLongClickable(false);
					}
				}
			} catch (ParseException e) {
				e.printStackTrace();
			}
			
			return convertView;
		}
	}
	
	class Beaconmsg_Adapter extends ArrayAdapter<String>{

		public Beaconmsg_Adapter() {
			super(getActivity(), android.R.layout.simple_list_item_1);
			// TODO Auto-generated constructor stub
		}
		@Override
		public int getCount() {			
			if(member.Rank.equals("3")){
				return beacon_msg.size();
			}else{
				return teacher_state.size();
			}			
		}
		
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			Beaconmsg_Holder bea_holder;
			
		if(convertView==null){
			convertView = getActivity().getLayoutInflater().inflate(R.layout.calendar_beacon_list_row, null);
			bea_holder = new Beaconmsg_Holder();
			bea_holder.tv = (TextView)convertView.findViewById(R.id.CALENDAR_BEACONMSG_TV);
			convertView.setTag(bea_holder);
			}else{
				bea_holder = (Beaconmsg_Holder)convertView.getTag();
			}		
		
			if(member.Rank.equals("3")){
			bea_holder.tv.setText((beacon_msg.size()-position)+". "+beacon_msg.get(position).Sta_Message+" "+beacon_msg.get(position).Sta_Date);
			}else{
			bea_holder.tv.setText((teacher_state.size()-position)+". "+teacher_state.get(position).Tea_Message+" "+teacher_state.get(position).Tea_Date);	
			}	
			
			return convertView;
		}		
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.CALENDAR_NEXT:
			Calendar_Grid(+1);
			select_date=0;
			teacher_state.clear();
			beacon_msg.clear();
			startNetwork(2);
			break;

		case R.id.CALENDAR_PREV:
			Calendar_Grid(-1);
			select_date=0;
			teacher_state.clear();
			beacon_msg.clear();
			startNetwork(2);			
			break;
		
		case R.id.CALENDAR_MAIN_DELETE:
			dialog_delete();
			break;
		}
		
	}
	
	class Calendar_Holder {
		TextView date;
		LinearLayout ly;
		LinearLayout ly_state;
		ImageView iv;
	}
	
	class Beaconmsg_Holder{
		TextView tv;
	}
	
	class Chinese_Date_Holder{
		String month;
		String day;
		String name;
	}
	
	class Chinese_Date {

		ArrayList<Chinese_Date_Holder> date;
		Chinese_Date_Holder cdh;
		ArrayList<Chinese_Date_Holder> getHoliday(int a) {
			date= new ArrayList<Chinese_Date_Holder>();			
			switch (a) {
			case 1:
				cdh = new Chinese_Date_Holder();
				cdh.month = "1";
				cdh.name = "신정";
				cdh.day ="1";
				date.add(cdh);
				break;
			case 3:
				cdh = new Chinese_Date_Holder();
				cdh.month = "3";
				cdh.name = "3.1절";
				cdh.day ="1";
				date.add(cdh);
				break;
			case 5:
				cdh = new Chinese_Date_Holder();
				cdh.month = "5";
				cdh.name = "어린이 날";
				cdh.day ="5";
				date.add(cdh);
				break;
			case 6:
				cdh = new Chinese_Date_Holder();
				cdh.month = "6";
				cdh.name = "현충일";
				cdh.day ="6";
				date.add(cdh);
				break;
			case 8:
				cdh = new Chinese_Date_Holder();
				cdh.month = "8";
				cdh.name = "광복절";
				cdh.day ="15";
				date.add(cdh);
				break;
			case 10:
				cdh = new Chinese_Date_Holder();
				cdh.month = "10";
				cdh.name = "개천절";
				cdh.day ="3";
				date.add(cdh);
				cdh = new Chinese_Date_Holder();
				cdh.month = "10";
				cdh.name = "한글날";
				cdh.day ="9";
				date.add(cdh);
				break;
			case 12:
				cdh = new Chinese_Date_Holder();
				cdh.month = "12";
				cdh.name = "크리스마스";
				cdh.day ="25";
				date.add(cdh);
				break;
			}
			return date;
		}
	}
	
/*	void dialog_write(){
		
		AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
		alert.setTitle("결석사유를 입력해주세요.");
		
		if(subject.equals("")){
			alert.setMessage(choose_day+"일 결석사유를 입력해주세요.");
		}
		
		 input = new EditText(getActivity());		 
		 alert.setView(input);		 
		 input.setText(subject);		 
		alert.setPositiveButton("확인", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int whichButton) {
				
				if(input.getText().toString().length()<1){
					
					Toast.makeText(getActivity(), "결석사유를 입력하지 않으셨습니다. 다시 입력해주세요.",Toast.LENGTH_SHORT).show();
					
				}else{
					startNetwork(3);	
				}
			}
		});

		alert.setNegativeButton("취소",
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int whichButton) {
						// Canceled.
					}
				});
		
		alert.show(); 
	}
*/	
	
	void dialog_delete(){
		AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
		alert.setTitle("삭제");
		
		if(subject.equals("")){
			alert.setMessage(choose_day+"일 결석사유를 삭제하시겠습니까?");
		}
		
		alert.setPositiveButton("예", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int whichButton) {
				startNetwork(4);
				
			}
		});

		alert.setNegativeButton("아니오",
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int whichButton) {
						// Canceled.
					}
				});
		
		alert.show(); 
	}
	
}
